package com.yanqu.road.dao.impl.activity.apprentice;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.apprentice.ApprenticeEffectBuffProject;
import com.yanqu.road.entity.activity.apprentice.data.ApprenticeBuffProjectData;
import com.yanqu.road.entity.activity.apprentice.data.ApprenticeUserData;
import com.yanqu.road.entity.activity.apprentice.data.ApprenticeUserExamData;
import com.yanqu.road.entity.activity.apprentice.data.ApprenticeUserPatronsTalentData;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class ApprenticeUserDataDaoImpl extends TempDataObjectDao<ApprenticeUserData> {

    public static String getApprenticeUserDataSql = "select * from `t_c_activity_apprentice_user_data` where `activityId`=?";
    public Map<Long, ApprenticeUserData> getApprenticeUserDataMap(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Long, ApprenticeUserData> map = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(getApprenticeUserDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                ApprenticeUserData data = parse(rs);
                map.put(data.getUserId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String updateApprenticeUserDataSql = "update `t_c_activity_apprentice_user_data` set serverId=?,unionId=?,level=?,programNum=?,talent1=?,talent2=?,talent3=?,talent4=?,talent5=?,waitProjects=?,buffProjectIds=?,examDiffNum=?,examData=?,startBanquetNum=?,canBeBanquetNum=?,nextEffectProject=?,createTime=? where activityId=? and userId=?";
    @Override
    protected boolean updateData(ApprenticeUserData apprenticeUserData) {
        apprenticeUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, apprenticeUserData.getServerId()));
        para.put(i++, new DbParameter(Types.VARCHAR, apprenticeUserData.getUnionId()));
        para.put(i++, new DbParameter(Types.INTEGER, apprenticeUserData.getLevel()));
        para.put(i++, new DbParameter(Types.INTEGER, apprenticeUserData.getProgramNum()));
        para.put(i++, new DbParameter(Types.BIGINT, apprenticeUserData.getTalent1()));
        para.put(i++, new DbParameter(Types.BIGINT, apprenticeUserData.getTalent2()));
        para.put(i++, new DbParameter(Types.BIGINT, apprenticeUserData.getTalent3()));
        para.put(i++, new DbParameter(Types.BIGINT, apprenticeUserData.getTalent4()));
        para.put(i++, new DbParameter(Types.BIGINT, apprenticeUserData.getTalent5()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(apprenticeUserData.getWaitProjects())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(apprenticeUserData.getBuffProjectIds())));
        para.put(i++, new DbParameter(Types.INTEGER, apprenticeUserData.getExamDiffNum()));
        String examStr = "";
        if (apprenticeUserData.getExamData() != null) {
            examStr = JSON.toJSONString(apprenticeUserData.getExamData());
        }
        para.put(i++, new DbParameter(Types.VARCHAR, examStr));
        para.put(i++, new DbParameter(Types.INTEGER, apprenticeUserData.getStartBanquetNum()));
        para.put(i++, new DbParameter(Types.INTEGER, apprenticeUserData.getCanBeBanquetNum()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(apprenticeUserData.getNextEffectProject())));
        para.put(i++, new DbParameter(Types.BIGINT, apprenticeUserData.getCreateTime()));
        para.put(i++, new DbParameter(Types.INTEGER, apprenticeUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, apprenticeUserData.getUserId()));
        if (getDb().executeNoneQuery(updateApprenticeUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertApprenticeUserDataSql = "insert into `t_c_activity_apprentice_user_data`(activityId,userId,serverId,unionId,level,programNum,talent1,talent2,talent3,talent4,talent5,waitProjects,buffProjectIds,examDiffNum,examData,startBanquetNum,canBeBanquetNum,nextEffectProject,createTime) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(ApprenticeUserData apprenticeUserData) {
        apprenticeUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, apprenticeUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, apprenticeUserData.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, apprenticeUserData.getServerId()));
        para.put(i++, new DbParameter(Types.VARCHAR, apprenticeUserData.getUnionId()));
        para.put(i++, new DbParameter(Types.INTEGER, apprenticeUserData.getLevel()));
        para.put(i++, new DbParameter(Types.INTEGER, apprenticeUserData.getProgramNum()));
        para.put(i++, new DbParameter(Types.BIGINT, apprenticeUserData.getTalent1()));
        para.put(i++, new DbParameter(Types.BIGINT, apprenticeUserData.getTalent2()));
        para.put(i++, new DbParameter(Types.BIGINT, apprenticeUserData.getTalent3()));
        para.put(i++, new DbParameter(Types.BIGINT, apprenticeUserData.getTalent4()));
        para.put(i++, new DbParameter(Types.BIGINT, apprenticeUserData.getTalent5()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(apprenticeUserData.getWaitProjects())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(apprenticeUserData.getBuffProjectIds())));
        para.put(i++, new DbParameter(Types.INTEGER, apprenticeUserData.getExamDiffNum()));
        String examStr = "";
        if (apprenticeUserData.getExamData() != null) {
            examStr = JSON.toJSONString(apprenticeUserData.getExamData());
        }
        para.put(i++, new DbParameter(Types.VARCHAR, examStr));
        para.put(i++, new DbParameter(Types.INTEGER, apprenticeUserData.getStartBanquetNum()));
        para.put(i++, new DbParameter(Types.INTEGER, apprenticeUserData.getCanBeBanquetNum()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(apprenticeUserData.getNextEffectProject())));
        para.put(i++, new DbParameter(Types.BIGINT, apprenticeUserData.getCreateTime()));
        if (getDb().executeNoneQuery(insertApprenticeUserDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected ApprenticeUserData parseData(ResultSet rs) throws SQLException {
        ApprenticeUserData apprenticeUserData = new ApprenticeUserData();
        apprenticeUserData.setActivityId(rs.getInt("activityId"));
        apprenticeUserData.setUserId(rs.getLong("userId"));
        apprenticeUserData.setServerId(rs.getLong("serverId"));
        apprenticeUserData.setUnionId(rs.getString("unionId"));
        apprenticeUserData.setLevel(rs.getInt("level"));
        apprenticeUserData.setProgramNum(rs.getInt("programNum"));
        apprenticeUserData.setTalent1(rs.getLong("talent1"));
        apprenticeUserData.setTalent2(rs.getLong("talent2"));
        apprenticeUserData.setTalent3(rs.getLong("talent3"));
        apprenticeUserData.setTalent4(rs.getLong("talent4"));
        apprenticeUserData.setTalent5(rs.getLong("talent5"));
        if (!StringUtils.isNullOrEmpty(rs.getString("waitProjects"))) {
            apprenticeUserData.setWaitProjects(JSON.parseObject(rs.getString("waitProjects"), new TypeReference<List<Integer>>() {}));
        }
        if (!StringUtils.isNullOrEmpty(rs.getString("buffProjectIds"))) {
            apprenticeUserData.setBuffProjectIds(JSON.parseObject(rs.getString("buffProjectIds"), new TypeReference<List<ApprenticeBuffProjectData>>() {}));
        }
        apprenticeUserData.setExamDiffNum(rs.getInt("examDiffNum"));
        if (!StringUtils.isNullOrEmpty(rs.getString("examData"))) {
            apprenticeUserData.setExamData(JSON.parseObject(rs.getString("examData"), ApprenticeUserExamData.class));
        }
        apprenticeUserData.setStartBanquetNum(rs.getInt("startBanquetNum"));
        apprenticeUserData.setCanBeBanquetNum(rs.getInt("canBeBanquetNum"));
        if (!StringUtils.isNullOrEmpty(rs.getString("nextEffectProject"))) {
            apprenticeUserData.setNextEffectProject(JSON.parseObject(rs.getString("nextEffectProject"), new TypeReference<List<ApprenticeEffectBuffProject>>() {}));
        }
        apprenticeUserData.setCreateTime(rs.getLong("createTime"));
        return apprenticeUserData;
    }
}
